package com.test;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.reactive.RedisReactiveCommands;

public class MultiDemo {
	private Logger log = LoggerFactory.getLogger(MultiDemo.class);
	private static StatefulRedisConnection<String, String> CONNECTION;
	private static RedisClient CLIENT;
	private static RedisReactiveCommands<String, String> REACTIVE_COMMAND;

	@BeforeClass
	public static void beforeClass() {
	    RedisURI redisUri = RedisURI.builder()
	            .withSentinel("10.16.34.125", 6406)
	            .withSentinelMasterId("sentinel-10.16.34.15-6381")
	            .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
	            .build();
	    CLIENT = RedisClient.create(redisUri);
	    CONNECTION = CLIENT.connect();
	    REACTIVE_COMMAND = CONNECTION.reactive();
	}

	@Test
	public void testReactiveFunctional() throws Exception {
	    REACTIVE_COMMAND.multi()
	    .doOnSuccess(r -> {
	        REACTIVE_COMMAND.set("counter", "1").doOnNext(x -> {
	        	log.info(x);
	        })
	        .subscribe();
	        REACTIVE_COMMAND.incr("counter").doOnNext(c -> {
	        	log.info(String.valueOf(c));
	        })
	        .subscribe();
	    })
	    .flatMap(s -> {
	    	return REACTIVE_COMMAND.exec();
	    })
	    .subscribe();
	    Thread.sleep(1000);
	}
	// OK
	// 2

}
